#include "stdafx.h"
#include "guiKeeper.h"
#include "sgGUI.h"
#include "BaseLayout/BaseLayout.h"
#include "MainMenu.h"
#include "ObjectManager.h"
#include "guiMediatorMyGUI.h"
#include "CityInfo.h"

namespace sgGUI {
    sgGUIKeeper::sgGUIKeeper()
    {
        guiMediatorMyGUI *m = new guiMediatorMyGUI();
        CObjectManager::Instance().SetGuiMediator( m );
    }

    void sgGUIKeeper::setupResources()
    {
        sgGUI::BaseManager::setupResources();
        addResourceLocation(getRootMedia() + "/Demos/Demo_Colour");
        addResourceLocation(getRootMedia() + "/Common/Demos");
    }

    void sgGUIKeeper::createScene()
    {
        MainMenu *mainmenu = new MainMenu( );
        std::string name = AttributeLayout_MainMenu.getData();
        m_layouts.insert( std::make_pair(name, (wraps::BaseLayout*)mainmenu ) );

        CityInfo *cityinfo = new CityInfo( );
        name = AttributeLayout_CityInfo.getData();
        m_layouts.insert( std::make_pair(name, (wraps::BaseLayout*)cityinfo ) );
    }

    void sgGUIKeeper::destroyScene()
    {
        for( LayoutMap::iterator iter = m_layouts.begin(); iter != m_layouts.end(); iter++ )
        {
            wraps::BaseLayout *l = iter->second;
            SAFE_DELETE(l);
        }
        m_layouts.clear();
    }

    sgGUIKeeper & sgGUIKeeper::Instance()
    {
        static sgGUIKeeper keeper;
        return keeper;
    }

    void sgGUIKeeper::hideAll()
    {
        for( LayoutMap::iterator iter = m_layouts.begin(); iter != m_layouts.end(); iter++ )
        {
            wraps::BaseLayout *layout = iter->second;
            layout->show( false );
        }
    }

    void sgGUIKeeper::showPanel( std::string &name )
    {
        wraps::BaseLayout *layout = getLayout( name );
        if( layout != nullptr )
        {
            layout->show( true );
        }
    }

    wraps::BaseLayout * sgGUIKeeper::getLayout( const std::string &name )
    {
        wraps::BaseLayout *result = nullptr;
        LayoutMap::iterator iter = m_layouts.find( name );
        if( iter != m_layouts.end() )
        {
            result = iter->second;
        }
        return result;
    }

}